package com.fingard.dsp.bank.directbank.ceb01;

import java.io.File;
import java.util.Date;

import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/12/29.
 */
public class CEB01ElecBillTrans extends CEB01Base implements IElecBillTrans {
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003069";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	1000	String	多个用“，”隔开,最多50个
            xmlTextWriter.writeElementString("BillId", query.detail.busiNo);
//            AcNo	账号	是	30	String
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            ProdNo	业务类型	是	3	String	D02-承兑
//            D03-提示收票
//            D04-背书转让
//            D05-质押
//            D06-解质押待收
//            D07-提示付款
            String prodNo;
            switch (Integer.valueOf(query.detail.respType)) {
                case 1:
                    prodNo = "D02";
                    break;
                case 2:
                    prodNo = "D03";
                    break;
                case 3:
                    prodNo = "D04";
                    break;
                case 5:
                    prodNo = "D06";
                    break;
                case 6:
                    prodNo = "D07";
                    break;
                default: {
                    throw new Exception("银行不支持该业务类型的签收应答：" + query.detail.respType);
                }
            }
            xmlTextWriter.writeElementString("ProdNo", prodNo);
//            Flag	操作状态	是	1	String	1-拒绝签收 0-同意签收
            String flag;
            if ("1".equals(query.detail.respFlag)) {
                flag = "0";
            } else {
                flag = "1";
            }
            xmlTextWriter.writeElementString("Flag", flag);
//            AcptResult	拒绝承兑理由	有条件的必输	100	String	当业务类型为“承兑”，操作状态为“拒绝签收”时必输
            if ("D02".equals(prodNo) && "1".equals(flag)) {
                xmlTextWriter.writeElementString("AcptResult", StringHelper.hasAnyChar(query.detail.unPayMemo) ? query.detail.unPayMemo : "无");
            } else {
                xmlTextWriter.writeElementString("AcptResult", query.detail.unPayMemo);
            }
//            IsSafety	是否申请商承保贴	有条件的必输	2	String	2-否------------------------- 当业务类型为“承兑”且操作状态为“同意签收”时必须上送
            if ("D02".equals(prodNo) && "0".equals(flag)) {
                xmlTextWriter.writeElementString("IsSafety", "2");
            } else {
                xmlTextWriter.writeElementString("IsSafety", "");
            }
//            Result	提示付款拒付理由	有条件的必输	256	String	业务类型为“提示付款”，操作状态为“拒绝签收”时必输 参考字典：拒付理由
            if ("D07".equals(prodNo) && "1".equals(flag)) {
                String unPayCode;
                if ("99".equals(query.detail.unPayCode)) {
                    unPayCode = "DC09";
                } else {
                    unPayCode = "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1);
                }
                xmlTextWriter.writeElementString("Result", unPayCode);
            } else {
                xmlTextWriter.writeElementString("Result", "");
            }
//            Reason	提示付款拒付理由说明	有条件的必输	256	String	如果“提示付款拒付理由”选择的是“其他”，则需要手写说明上送
            xmlTextWriter.writeElementString("Reason", query.detail.unPayMemo);
//            Mobile	手机号码	否	11	String	票据提交笔数大于1，手机号码必须为空
            xmlTextWriter.writeElementString("Mobile", "");

            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003015";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	20	String
            xmlTextWriter.writeElementString("BillId", "");// TODO: 2019/1/3
//            AcNo	账号	是	30	String	必须是企业网银的签约账号
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003010";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	20	String
            xmlTextWriter.writeElementString("BillId", query.detail.busiNo);// TODO: 2019/1/2
//            AcNo	账号	是	30	String	支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            Mobile	收款人手机号	否	11	String
            xmlTextWriter.writeElementString("Mobile", "");
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003019";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	20	String
            xmlTextWriter.writeElementString("BillId", query.detail.busiNo);
//            Isendo	转让标记	是	4	String
            String tranFlag;
            if ("1".equals(query.detail.transFlag)) {
                tranFlag = "EM00";
            } else {
                tranFlag = "EM01";
            }
            xmlTextWriter.writeElementString("Isendo", tranFlag);
//            AcNo	背书人账号	是	30	String	必须是企业网银的签约账号支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            AcName2	被背书人名称	否	50	String	行外时为必须项
            xmlTextWriter.writeElementString("AcName2", query.detail.receName);
//            AcNo2	被背书人账号	是	30	String
            xmlTextWriter.writeElementString("AcNo2", query.detail.receAct);
//            BankType	被背书人行内行外标示	是	1	String	0-	光大银行 1-	非光大银行
            String isSameBnk;
            if ("1".equals(query.detail.receSameBank)) {
                isSameBnk = "0";
            } else {
                isSameBnk = "1";
            }
            xmlTextWriter.writeElementString("BankType", isSameBnk);
//            ToEndorseBankNo	被背书人开户行行号	否	30	String	行外时为必须项
            xmlTextWriter.writeElementString("ToEndorseBankNo", query.detail.receBnkCode);
//            Mobile	被背书人手机号码	否	11	String
            xmlTextWriter.writeElementString("Mobile", "");

            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003045";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            AcNo	持票人账号	是	30	String	支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            PawneeType	质权人类别	是	1	String	1：银行 2：非银行
            String pawneeType = getBankConfig().getMapValueByKey("PawneeType", query.detail.receAct);
            xmlTextWriter.writeElementString("PawneeType", pawneeType);
//            PawneeBankType	质权人开户行类型	是	1	String	1：光大银行 2：非光大银行
            xmlTextWriter.writeElementString("PawneeBankType", query.detail.receSameBnk);
            if ("1".equals(query.detail.receSameBnk)) {
                //            PawnBankBrchNo	非银行时是：质权人开户机构；银行时：输入质权银行机构	否	4	String	行内时必须
                xmlTextWriter.writeElementString("PawnBankBrchNo", query.detail.receBnkCode);
                //            PawnBankBrchName	质权银行机构名称	否	30	String	质权人类别为“银行”时为必须项
                xmlTextWriter.writeElementString("PawnBankBrchName", "");
                xmlTextWriter.writeElementString("PawneeAccount", "");
                xmlTextWriter.writeElementString("PawneeBankName", "");
                xmlTextWriter.writeElementString("PawneeBankNo", "");
            } else {
                //            PawnBankBrchNo	非银行时是：质权人开户机构；银行时：输入质权银行机构	否	4	String	行内时必须
                xmlTextWriter.writeElementString("PawnBankBrchNo", "");
                xmlTextWriter.writeElementString("PawnBankBrchName", "");
                //            PawneeAccount	质权人帐号	否	30	String	非银行时是必须项
                xmlTextWriter.writeElementString("PawneeAccount", query.detail.receAct);
                //            PawneeBankName	质权人账号开户行名称	否	50	String	质权人类别为“非银行”是必须项
                xmlTextWriter.writeElementString("PawneeBankName", query.detail.receBnkName);
                //            PawneeBankNo	银行时是：质权银行行号；非银行时是：质权人开户行行号	否	12	String	质权人类别为“非银行”同时开户行为非光大时是必须项 |质押给非光大银行时质权行行号必输项
                xmlTextWriter.writeElementString("PawneeBankNo", query.detail.receBnkCode);
            }
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003022";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	20	String
            xmlTextWriter.writeElementString("BillId", query.detail.busiNo);// TODO: 2019/1/2
//            AcNo	持票人账号	是	30	String	加挂网银，签约账号 支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            Liquidation	清算方式	是	2	String	见数据字典（票据系统保证清算方式的正确性）
            xmlTextWriter.writeElementString("Liquidation", "1".equals(query.detail.payType) ? "1" : "0");
//            Overdoseresults	逾期发托理由	否	60	String	是逾期时必填（票据判断）
            if ("2".equals(query.detail.proPayType)) {
                xmlTextWriter.writeElementString("Overdoseresults", query.detail.overDueRea);
            } else {
                xmlTextWriter.writeElementClosed("Overdoseresults");
            }
//            Mobile	承兑人手机号	否	11	String
            xmlTextWriter.writeElementClosed("Mobile");
//            Notes	备注	否	50	String
            xmlTextWriter.writeElementString("Notes", StringHelper.hasAnyChar(query.detail.memo) ?
                    query.detail.memo : "无");
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        retAts.writeStart();
        RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
        try {
            query.readRecord();
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.TransCode = "b2e003070";
            header.batchID = query.detail.reqSeqID;
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlTextWriter, header);
//            TransContent?ReqData
            xmlTextWriter.writeStartElement("TransContent");
            xmlTextWriter.writeStartElement("ReqData");
//            ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlTextWriter.writeElementString("ClientPatchID", query.detail.reqSeqID);
//            BillId	票据ID	是	1000	String	多个用“，”隔开,最多50个
            xmlTextWriter.writeElementString("BillId", query.detail.busiNo);
//            AcNo	账号	是	30	String
            xmlTextWriter.writeElementString("AcNo", query.detail.actNum);
//            ProdNo	业务类型	是	3	String
            String prodNo;
            switch (Integer.valueOf(query.detail.cancelType)) {
                //    撤回类型 1-承兑申请撤回 2-提示收票申请撤回 3-背书转让申请撤回 4-贴现申请撤回 5-质押申请撤回 6-（逾期）提示付款撤回
//    7-出票保证申请撤回 8-承兑保证申请撤回  9-背书保证申请撤回 10-追索通知撤回 11-同意清偿撤回 12-解质押申请撤回
                case 1:
                    prodNo = "D02";
                    break;
                case 2:
                    prodNo = "D03";
                    break;
                case 3:
                    prodNo = "D04";
                    break;
                case 4:
                    prodNo = "D08";
                    break;
                case 5:
                    prodNo = "D05";
                    break;
                case 6:
                    prodNo = "D07";
                    break;
                default:
                    throw new Exception("暂不支持其他操作类型的撤销：" + query.detail.cancelType);
            }
            xmlTextWriter.writeElementString("ProdNo", prodNo);
//            D02-承兑
//            D03-提示收票
//            D04-背书转让
//            D05-质押
//            D06-解质押待收
//            D07-提示付付款
//            D08-贴现
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlTextWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnMsg = tmpbkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = returnCode;
                        String outTranId = tmpbkReader.getFirstText("/Transaction/TransContent/RespData/ClientPatchID");
                        String outTranFile = getBankConfig().getValueAsString("SaveOutTranId");
                        String fileContext = query.detail.reqSeqID + "_" + outTranId;
                        FileHelper.writeFile(outTranFile + File.separator + fileContext, fileContext, getCharset());
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = returnCode + " : " + returnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        } catch (Exception ex) {
            detail.respInfo = ex.toString();
            detail.respCode = "-1";
            WriteBankLogLn(ex);
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        return null;
    }
}
